package at.fhooe.stefanirndorfer.quadtree;

import static org.lwjgl.opengl.GL11.GL_LINE_LOOP;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2d;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;

import at.fhooe.stefanirndorfer.entity.Entity;
import at.fhooe.stefanirndorfer.game.Game;
import at.fhooe.stefanirndorfer.labeling.Region;

public class QuadTreeNode extends Entity {
	
	private static int numLeafs = 0;

	private double size;
	private double[] center;
	private QuadTreeNode[] childNodes;
	private QuadTreeNode father;
	private boolean isWall;
	private Region label;
	private short sonType;
	private int leafNumber;
	//private TrueTypeFont font;
	//private FontManager fm;
	
	

	
	public QuadTreeNode(double size2, double[] center, QuadTreeNode father, short sonType) {
		super(size2, size2, center[0]-size2/2, center[1]+size2/2);
		this.size = size2;
		this.center = center;
		this.father = father;
		childNodes = new QuadTreeNode[4]; //child nodes in order: NW, NE, SE, SW
		isWall = false;
		label = null;
		this.sonType = sonType;
		leafNumber = -1;
		//fm = Game.getInstance().getFontManager();
	}
	

	
	//////////////////////////////////////////////////
	// Getters and Setters
	//////////////////////////////////////////////////
	
	public void setIsLeaf(){
		if(!isWall){			
			numLeafs++;
			this.leafNumber = numLeafs;
		}
	}

	public static int getNumLeafs(){
		return numLeafs;
	}
	
	public void setLeafNumber(int leafNumber){
		this.leafNumber = leafNumber;
	}
	
	public int getLeafNumber(){
		return leafNumber;
	}

	public double getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public double[] getCenter() {
		return center;
	}

	public void setCenter(double[] center) {
		this.center = center;
	}

	public QuadTreeNode[] getChildNodes() {
		return childNodes;
	}

	public void setChildNodes(QuadTreeNode[] childNodes) {
		this.childNodes = childNodes;
	}
	
	public boolean isWall() {
		return isWall;
	}
	
	public void setIsWall(boolean isWall) {
		this.isWall = isWall;
	}
	
	public short getSontype() {
		return sonType;
	}
	

	//////////////////////////////////////////////////
	// Overridden Methods
	//////////////////////////////////////////////////
	

	public QuadTreeNode getFather() {
		return father;
	}

	public void setFather(QuadTreeNode father) {
		this.father = father;
	}

	

	@Override
	public void update(int delta) {
		
	}


	@Override
	public double[] getPosition() {
		
		return null;
	}


	@Override
	public void render() {
		if(childNodes[0] == null){
			if(!isWall){
				//Log.info(this, "PosX: " + posX + ", PosY: " + posY + ", Size: " + width);
				
				//glColor3f(0.909f, 0.627f, 0.184f);
				glColor3f(label.getColor()[0], label.getColor()[1], label.getColor()[2]);
				
				glBegin(GL_QUADS);
				glVertex2d(posX, posY);
				glVertex2d(posX, posY - height);
				glVertex2d(posX + width, posY - height);
				glVertex2d(posX + width, posY);
				glEnd();
				
				if(Game.getInstance().getFontManager().getFont() != null && leafNumber == 1){
					GL11.glEnable(GL11.GL_TEXTURE_2D);
					//int w = Game.getInstance().getWindowWidth();
					//int h = Game.getInstance().getWindowHeight();
					//GL11.glOrtho(-w/2, w/2, -h/2, h/2, 1, -1);
					//Color.white.bind();
					Game.getInstance().getFontManager().getFont().drawString((float)center[0], (float)center[1], ""+leafNumber, Color.black);
					//GL11.glOrtho(-w/2, w/2, h/2, -h/2, 1, -1);
					GL11.glDisable(GL11.GL_TEXTURE_2D);
				}
				
			} else{
				
			glColor3f(0.317f, 0.419f, 0.909f);
			glBegin(GL_LINE_LOOP);
			//glBegin(GL_QUADS);
			glVertex2d(posX, posY);
			glVertex2d(posX, posY - height);
			glVertex2d(posX + width, posY - height);
			glVertex2d(posX + width, posY);
			glEnd();
			
			}
			
		}
	}

	
	public void setLabel(Region label){
		this.label = label;
	}
	
	public Region getLabel(){
		return label;
	}
	

	/**
	 * 
	 * @returns true if a label is already assigned
	 */
	public boolean isLabeled() {
		return label != null;
	}

	

	
}
